using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using SigmaStock_Components;

namespace project
{
    public partial class AddStock : Form
    {
        public List<int> magazine_sno = new List<int>();

        struct order_magazine
        {
            public string MNAME;
            public DateTime MDATE;
            public DateTime SDATE;
            public int OPENING_STOCK;
            public int CLOSING_STOCK;            
            
        }

        OleDbCommand command;
        OleDbConnection conn;
        string query;
       

        public AddStock()
        {
            command = new OleDbCommand();
            conn=DBComponent.Mag_DealerDB.dbconnection();
            conn.Open();
            command.Connection = conn;
            textBox3.Visible = false;
            InitializeComponent();
            //textBox1.Text = String.Format("{0:dd/MM/yyyy}",DateTime.Now);
            UpdateDataSource();
        }

        private void UpdateDataSource()
        {
            String s;
            s = "Select * from order_magazine";
            OleDbCommand cmd1 = new OleDbCommand(s, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "order_magazine");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "order_magazine";
            ordermagazineBindingSource.ResetBindings(false);
            this.Refresh();

            mag_name.Text = "";
            textBox5.Text = "";
            textBox2.Text = "";
            textBox1.Text = "";
        }



        private void mag_name_KeyPress(object sender, KeyPressEventArgs e)
        {
            /*if (e.KeyChar == (char)13)
            {
                query = "select MDATE from order_magazine where MNAME='"+mag_name.Text+"';";
                command.CommandText = query;
                OleDbDataReader reader=command.ExecuteReader();
                while (reader.Read())
                {
                    DateTime dt = DateTime.Parse(reader["MDATE"].ToString());                    
                    string dt1=dt.Day+"/"+dt.Month+"/"+dt.Year;                    
                    comboBox1.Items.Add(dt1);

                }
                reader.Close();
            }*/

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int op_stock = int.Parse(textBox2.Text.ToString());
            order_magazine newmag = new order_magazine();
            newmag.MNAME = mag_name.Text;
            newmag.MDATE = DateTime.Parse(textBox5.Text);
            newmag.SDATE = DateTime.Parse(textBox1.Text);
            newmag.OPENING_STOCK = int.Parse(textBox2.Text);
            newmag.CLOSING_STOCK = newmag.OPENING_STOCK;
            string query = "insert into stock_date(MNAME,MDATE,SDATE,OPENING_STOCK,CLOSING_STOCK) VALUES('" + newmag.MNAME + "','" + newmag.MDATE + "','" + newmag.SDATE+ "','" + newmag.OPENING_STOCK +"','"+newmag.CLOSING_STOCK+ "');";
            command.CommandText=query;
            command.ExecuteNonQuery();

            query="delete from order_magazine where S_NO="+int.Parse(textBox4.Text)+";";
            command.CommandText = query;
            command.ExecuteNonQuery();

            query = "insert into magazine_selling(MNAME,MDATE) values('" + newmag.MNAME + "',#" + newmag.MDATE + "#);";
            command.CommandText = query;
            command.ExecuteNonQuery();
            String s;
            s = "Select * from order_magazine";
            OleDbCommand cmd1 = new OleDbCommand(s, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "order_magazine");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "order_magazine";
            ordermagazineBindingSource.ResetBindings(false);
            this.Refresh();

            mag_name.Text = "";
            textBox5.Text = "";
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void AddStock_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mag_dealerDataSet.order_magazine' table. You can move, or remove it, as needed.
            this.order_magazineTableAdapter.Fill(this.mag_dealerDataSet.order_magazine);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ValidateStock())
            {
                query = "select * from order_magazine where S_NO=" + textBox4.Text + ";";
                command.CommandText = query;
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    mag_name.Text = reader[1].ToString();
                    textBox5.Text = reader[2].ToString();
                    textBox1.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToString();
                    textBox2.Text = reader[4].ToString();

                }
                reader.Close();

            }
        }

        private bool ValidateStock()
        {
            if (String.IsNullOrEmpty(textBox4.Text) || !(Validation.IsPositiveNumber(textBox4.Text)))
            {
                textBox3.Visible = true;
                Console.Beep();
                textBox3.Text = "Check Serial Number";
                return false;

            }

            query = "select S_NO from order_magazine;";
                command.CommandText = query;
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    magazine_sno.Add(int.Parse(reader[0].ToString()));
                }
                reader.Close();
                if (magazine_sno.Contains(int.Parse(textBox4.Text)))
                {
                    return true;
                }
                else
                {
                    textBox3.Visible = true;
                    Console.Beep();
                    textBox3.Text = "Serial Number Doesn't Occurs";
                    return false;
                }
                

        }

        private void AddStock_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }    
        
    }
}